package geektime.spring.springbucks;

import geektime.spring.springbucks.mapper.CoffeeMapper;
import geektime.spring.springbucks.model.Coffee;
import geektime.spring.springbucks.model.OrderState;
import geektime.spring.springbucks.service.CoffeeOrderService;
import geektime.spring.springbucks.service.CoffeeService;
import lombok.Builder;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.List;
import java.util.Map;


@Slf4j
@SpringBootApplication
@EnableTransactionManagement
@EnableCaching(proxyTargetClass = true)
@MapperScan("geektime.spring.springbucks.mapper")
public class SpringBucksApplication implements CommandLineRunner {

	@Autowired
	private CoffeeService coffeeService;
	 @Autowired
	private CoffeeOrderService orderService;

	@Autowired
	private CoffeeMapper coffeeMapper;

	@Autowired
	private DataSource dataSource;
	@Autowired
	private JdbcTemplate jdbcTemplate;






	public static void main(String[] args) {
		SpringApplication.run(SpringBucksApplication.class, args);
	}

	@Override
	public void run(String... args) throws Exception {
 log.info(dataSource.toString());


		log.info("=========第一页所有=======");
		coffeeService.reloadCoffee( 1,0).forEach(c-> log.info("Page(1) Coffee {}",c) );
		log.info("=========第一页所有缓存=======");
		for(int i=0;i<6 ;i++) {
			boolean b = i == 0;
			if(b){
				log.info("=========第一次读取数据库=======");
			}else{
				log.info("=========第"+i+"次读取缓存=======");
			}
			coffeeService.findAllWithRowBounds1(1, 0).forEach(c -> log.info("Page(1) Coffee {}", c));
		}
		log.info("=========第一页所有缓存清除=======");
		coffeeService.reloadCoffee( 1,0).forEach(c-> log.info("Page(1) Coffee {}",c) );
		log.info("=========第一页前三条=======");
		coffeeService.findAllWithRowBounds(1,3).forEach(c-> log.info("Page(1) Coffee {}",c) ) ;
		log.info("=========第二页前两条=======");
		coffeeService.findAllWithRowBounds(2,3).forEach(c-> log.info("Page(2) Coffee {}",c) ) ;
		log.info("=========获取latte=======");
		Coffee d=coffeeService.findOneCoffee("latte");
		log.info("Coffee {}",d);
	 	 log.info("=========下单成功=======");
		Long id=orderService.createOrder("lilet",d);
		log.info("=========查询订单=======");
		List<Map> m=orderService.selectorder(id);
		m.forEach(c->log.info("Page(1) Order {}",c));
		log.info("=========修改订单状态=======");
		orderService.updateState(OrderState.PAID,id);
		log.info("=========修改订单状态失败=======");
		orderService.updateState(OrderState.INIT,id);
		log.info("=========删除失败回滚=======");
		try{
			orderService.deleteorderbak(id);
		}catch(Exception e){
			m=orderService.selectorder(id);
			m.forEach(c->log.info("Order {}",c));
		}
		log.info("count {}",orderService.selectordercount(id));

		log.info("=========删除订单=======");
		orderService.deleteorder(id);
		m=orderService.selectorder(id);
		m.forEach(c->log.info("Order {}",c));
		log.info("count {}",orderService.selectordercount(id));


		 log.info("=========SQL中IN判断=======");
	 try {
			coffeeService.findAllWithRowBounds3("t_coffee").forEach(c-> log.info("Page(2) Coffee {}",c) ) ;
		 }catch (Exception e){
			log.info("=========SQL中含IN数据超过10条=======");
		}


		log.info("=========SQL注入=======");
		try {
			coffeeService.findAllWithRowBounds2("1 or 1=1").forEach(c-> log.info("Page(2) Coffee {}",c) ) ;
		}catch (Exception e){
			log.info("=========SQL注入拦截=======");
		}
		log.info("=========SQL正常情况=======");
		coffeeService.findAllWithRowBounds2("1").forEach(c-> log.info("Page(2) Coffee {}",c) ) ;



	}

}

